﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.ServiceModel;
using NCacheDClient.Exceptions;
using NCacheDClient.Configuration;
using NCacheDClient.NCacheDServer;
using NCacheDClient.Internal.Clients;

namespace NCacheDClient.Internal
{
    internal sealed class NCacheDServerStore
    {

        public static int WeightFactor = 100;


        internal ICacheClient GetServerByKey(string key)
        {
            int lookupKey = Math.Abs(key.GetHashCode()) % WeightFactor;
            return ServerLookup[lookupKey];
        }


        private ICacheClient[] serverLookup;
        private object _serverlookuplock = new object();

        private ICacheClient[] ServerLookup
        {
            get
            {
                if (serverLookup == null)
                {
                    lock (_serverlookuplock)
                    {
                        if (serverLookup == null)
                        {
                            serverLookup = ServerLookupBuilder.CreateServerLookup();
                        }
                    }
                }
                return serverLookup;
            }
        }

    }
}
